package com.prs.crm.action.oa;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.context.Attach;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.CompanyPicture;
import com.prs.crm.domain.oa.Picture;
import com.prs.crm.service.oa.CompanyPictureService;
import com.prs.crm.service.oa.PictureService;

public class CompanyPictureAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private PaginationSupport pagination;
	private CompanyPicture companyPicture;
	private CompanyPictureService companyPictureService;
	private Picture picture;
	private PictureService pictureService;
	private File upload;
	private String uploadFileName;
	private Integer[] pictureIds;
	
	@Action("listCompanyPictureMaintain")
	public String listCompanyPictureMaintain() {
		Person currPerson=AppContext.getAccountContext().currentUser();
		List<CompanyPicture> cp=this.getCompanyPictureService().geAllCompanyPicture();
		for (CompanyPicture cps : cp) {
			if(cps.getRecorder().getResume().getName().equals(currPerson.getResume().getName())){
				cps.setUpState(1);//可见
				this.getCompanyPictureService().submit(cps);
			}else{
				if (cps.getWritePerson()==null) {
					cps.setUpState(0);//不可见
					this.getCompanyPictureService().submit(cps);
				}else{
					String[] per=cps.getWritePerson().split(",");
					for (String pers : per) {
						if (currPerson.getResume().getName().equals(pers)) {
							cps.setUpState(1);//可见
							this.getCompanyPictureService().submit(cps);
							break;
						}else{
							cps.setUpState(0);//不可见
							this.getCompanyPictureService().submit(cps);
						}
					}
				}
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				CompanyPicture.class);
		criteria.add(Restrictions.eq("upState", 1));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.companyPictureService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listCompanyPictureLook")
	public String listCompanyPictureLook() {
		Person currPerson=AppContext.getAccountContext().currentUser();
		List<CompanyPicture> cp=this.getCompanyPictureService().geAllCompanyPicture();
		for (CompanyPicture cps : cp) {
			if(cps.getRecorder().getResume().getName().equals(currPerson.getResume().getName())){
				cps.setSeeState(1);//可见
				this.getCompanyPictureService().submit(cps);
			}else{
				if (cps.getLookPerson()==null) {
					cps.setSeeState(0);//不可见
					this.getCompanyPictureService().submit(cps);
				}else{
					String[] per=cps.getLookPerson().split(",");
					for (String pers : per) {
						if (currPerson.getResume().getName().equals(pers)) {
							cps.setSeeState(1);//可见
							this.getCompanyPictureService().submit(cps);
							break;
						}else{
							cps.setSeeState(0);//不可见
							this.getCompanyPictureService().submit(cps);
						}
					}
				}
			}
		}
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				CompanyPicture.class);
		criteria.add(Restrictions.eq("seeState", 1));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.companyPictureService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listMove")
	public String listMove() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				CompanyPicture.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.companyPictureService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listPicture")
	public String listPicture() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Picture.class);
		criteria.add(Restrictions.eq("picture", companyPicture));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.pictureService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listPictureLook")
	public String listPictureLook() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Picture.class);
		criteria.add(Restrictions.eq("picture", companyPicture));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.pictureService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("editCompanyPicture")
	public String editCompanyPicture() {
		
		return SUCCESS;
	}
	@Action("editPicture")
	public String editPicture() {
		
		return SUCCESS;
	}
	@Action("viewPicture")
	public String viewPicture() {
		
		return SUCCESS;
	}
	@Action(value = "saveCompanyPicture", results = {
			@Result(name = "success", type = "redirect", location = "listCompanyPictureMaintain"),
			@Result(name = "error", type = "chain", location = "editCompanyPicture?companyPicture=${companyPicture.getId()}") })
	public String saveCompanyPicture(){
		if(this.companyPicture.getId()==null){
			companyPicture.setNum(0);
			this.companyPictureService.save(companyPicture);
		}else{
			this.companyPictureService.submit(companyPicture);
		}
		return SUCCESS;
	}
	@Action(value = "saveCover", results = {
			@Result(name = "success", type = "redirect", location = "listPicture?companyPicture=${companyPicture.getId()}"),
			@Result(name = "error", type = "chain", location = "listPicture?companyPicture=${companyPicture.getId()}") })
	public String saveCover(){
		this.companyPicture.setCoverPicture(this.picture.getPic());
		this.companyPictureService.submit(companyPicture);
		return SUCCESS;
	}
	@Action(value = "movePicture", results = {
			@Result(name = "success", type = "redirect", location = "listPicture?companyPicture=${companyPicture.getId()}"),
			@Result(name = "error", type = "chain", location = "listMove?picture=${picture.getId()}&companyPicture=${companyPicture.getId()}") })
	public String movePicture(){
		this.companyPicture=this.companyPictureService.get(pictureIds[0]);
		this.picture.setPicture(companyPicture);
		this.pictureService.submit(picture);
		return SUCCESS;
	}
	@Action(value = "savePicture", results = {
			@Result(name = "success", type = "redirect", location = "listPicture?companyPicture=${companyPicture.getId()}"),
			@Result(name = "error", type = "chain", location = "editPicture}") })
	public String savePicture() throws IOException{
		if(this.upload!=null){
		Attach attach = FileUtil.getInstance().saveFile(upload, uploadFileName, "", Constrains.UPLOAD_DIR_SCHEDULE);
		if (attach.getContentType()!="gif" || attach.getContentType()!="jpg" || attach.getContentType()!="bmp" || attach.getContentType()!="png") {
			this.addActionError("上传图片格式不正确，请上传格式为jpg、gif、png、bmp的图片");
			return ERROR;
		}	
		if(attach.getSrc() != null) {
				picture.setName(attach.getName());
				picture.setPic(attach.getSrc());
				if (companyPicture.getNum()==0) {
					companyPicture.setCoverPicture(attach.getSrc());
				}	
			}
		}
		Person person = AppContext.getAccountContext().currentUser();
		picture.setRecorder(person);
		picture.setPicture(companyPicture);
		this.pictureService.save(picture);
		companyPicture.setNum(companyPicture.getNum()+1);
		this.companyPictureService.submit(companyPicture);
		return SUCCESS;
	}
	@Action(value = "removePicture", results = {
			@Result(name = "success", type = "redirect", location = "listCompanyPictureMaintain"),
			@Result(name = "error", type = "chain", location = "listCompanyPictureMaintain") })
	public String removePicture(){
		for(int i=0;i<companyPicture.getPictures().size();i++){
			Picture p=companyPicture.getPictures().get(i);
			this.pictureService.remove(p);
		}
		this.companyPictureService.remove(companyPicture);
		return SUCCESS;
	}
	@Action(value = "removePicture2", results = {
			@Result(name = "success", type = "redirect", location = "listCompanyPictureMaintain"),
			@Result(name = "error", type = "chain", location = "listCompanyPictureMaintain") })
	public String removePicture2(){
		this.pictureService.remove(picture);
		this.picture.getPicture().setNum(picture.getPicture().getNum()-1);
		this.getCompanyPictureService().submit(picture.getPicture());
		return SUCCESS;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public CompanyPicture getCompanyPicture() {
		return companyPicture;
	}
	public void setCompanyPicture(CompanyPicture companyPicture) {
		this.companyPicture = companyPicture;
	}
	public CompanyPictureService getCompanyPictureService() {
		return companyPictureService;
	}
	public void setCompanyPictureService(CompanyPictureService companyPictureService) {
		this.companyPictureService = companyPictureService;
	}
	public Picture getPicture() {
		return picture;
	}
	public void setPicture(Picture picture) {
		this.picture = picture;
	}
	public PictureService getPictureService() {
		return pictureService;
	}
	public void setPictureService(PictureService pictureService) {
		this.pictureService = pictureService;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public Integer[] getPictureIds() {
		return pictureIds;
	}
	public void setPictureIds(Integer[] pictureIds) {
		this.pictureIds = pictureIds;
	}
	
}
